package com.seal.yoyoshop.adminweb;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.service.GoodsService;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.GoodsServiceImpl;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DeleteTypeServlet", value = "/admin/typeDelete")
public class DeleteTypeServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();
    private GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idstr = request.getParameter("id");
        Integer id = Integer.parseInt(idstr);
        Type type = typeService.getTypeById(id);
        List<Goods> goodsList = goodsService.getGoodsListByTypeId(id);
        if(goodsList.size() == 0){

            boolean isSuccess = typeService.deleteType(type);
            if (isSuccess){
                request.setAttribute("msg","删除成功");
            }else {
                request.setAttribute("msg","删除失败");
            }
        }else {
            request.setAttribute("msg","删除失败,该大类存在商品");
        }


        request.setAttribute("flag",4);
        List<Type> typeList = typeService.getTypeList();
        request.setAttribute("typeList",typeList);
        request.getRequestDispatcher("/admin/type_list.jsp").forward(request,response);

    }

}
